package com.tianqu.tms.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.tianqu.tms.core.base.TmsEntity;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.dao.TmsDao;
import com.tianqu.tms.dao.VehicleBrandDao;
import com.tianqu.tms.dto.VehicleBrandDto;

/**
 * 车系字典表
 * 
 * @author tianshifu
 *
 */

public class VehicleBrand extends TmsEntity {

	/**
	 * 
	 */
	private static final long serialVersionUID = -9220498913684893879L;

	private String brandId;// 车系ID

	private String brandName;// 车系名称

	private String firstChar;// 车系名英文首写字母

	private String brandPinYin;// 拼音

	private String vendorId;// 车辆品牌ID
	
	/**
	 * 实例化codes表
	 * 
	 * @return
	 */
	private static VehicleBrand instance;

	List<VehicleBrand> codes = null;
	
	/**
	 * 加载Codes
	 * 
	 * @return
	 * @throws TmsDaoException
	 */
	public static VehicleBrand getInstance() throws TmsDaoException {
		if (null == instance) {
			instance = new VehicleBrand();
			instance.init();
		}
		return instance;
	}

	/**
	 * 实例化Code
	 * 
	 * @throws TmsDaoException
	 */
	public void init() throws TmsDaoException {
		codes = new ArrayList<VehicleBrand>();
		setCodes();
	}
	
	/**
	 * 获取Codes信息存在静态codes中
	 * @throws TmsDaoException
	 */
	public void setCodes() throws TmsDaoException {
		VehicleBrandDao dao = (VehicleBrandDao) TmsDao.getInstance(TmsDao.TMS_VEHICLE_BRNAD);
		List<VehicleBrandDto> list = dao.findAllVehicleBrandInfo();
		for (int i = 0; i < list.size(); i++) {
			VehicleBrandDto dto = (VehicleBrandDto) list.get(i);
			VehicleBrand entity = new VehicleBrand();
			entity.setBrandId(dto.getBrandId());
			entity.setBrandName(dto.getBrandName());
			entity.setBrandPinYin(dto.getBrandPinYin());
			entity.setFirstChar(dto.getFirstChar());
			entity.setVendorId(dto.getVendorId());
			codes.add(entity);
		}
	}
	/**
	 * 根据品牌ID获取车系下拉列表
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List getAllVehicleBrandInfo(String vendorId)
	{
		List list = new ArrayList();
		Map map = null;
		for(int i=0;i<codes.size();i++)
		{
			VehicleBrand entity = codes.get(i);
			if(entity.getVendorId().equals(vendorId))
			{
				map = new HashMap();
				map.put("brandId", entity.getBrandId());
				map.put("brandName", entity.getBrandName());
				list.add(map);
			}
		}
		return list;
	}
	
	/**
	 * 根据车系ID获取品牌ID
	 * @return
	 */
	public String getVendorIdByBrandId(String brandId)
	{
		String vendorId = "";
		for(int i=0;i<codes.size();i++)
		{
			VehicleBrand entity = codes.get(i);
			if(entity.getBrandId().equals(brandId))
			{
				vendorId = entity.getVendorId();
			}
		}
		return vendorId;
	}

	/**
	 * 根据brandId查询vendorId
	 * @param brandId
	 * @return
	 * @throws TmsDaoException
	 */
	public String getVendorId(String brandId) throws TmsDaoException{
		VehicleBrandDao dao = (VehicleBrandDao) TmsDao.getInstance(TmsDao.TMS_VEHICLE_BRNAD);
		
		String vendorId = dao.findVendorIdByBrandId(brandId);
		return vendorId;
	}

	public String getBrandName() {
		return brandName;
	}

	public void setBrandName(String brandName) {
		this.brandName = brandName;
	}

	public String getFirstChar() {
		return firstChar;
	}

	public void setFirstChar(String firstChar) {
		this.firstChar = firstChar;
	}

	public String getBrandPinYin() {
		return brandPinYin;
	}

	public void setBrandPinYin(String brandPinYin) {
		this.brandPinYin = brandPinYin;
	}


	public String getBrandId() {
		return brandId;
	}

	public void setBrandId(String brandId) {
		this.brandId = brandId;
	}

	public String getVendorId() {
		return vendorId;
	}

	public void setVendorId(String vendorId) {
		this.vendorId = vendorId;
	}

	@Override
	public void save() throws TmsDaoException, TmsException {
		// TODO Auto-generated method stub

	}

}
